package com.knife.blog.web.action;

import com.knife.blog.domain.BlogInfo;
import com.knife.blog.domain.BlogUser;
import com.knife.blog.logic.BlogUserService;
import com.knife.blog.logic.CurrentUser;
import com.knife.blog.logic.SysConfig;
import com.knife.blog.logic.impl.BlogCoreServiceImpl;
import com.knife.blog.logic.impl.BlogMessageServiceImpl;
import com.knife.blog.logic.impl.BlogResourceServiceImpl;
import com.knife.blog.logic.impl.BlogReviewServiceImpl;
import com.knife.blog.logic.impl.BlogUserServiceImpl;
import com.knife.blog.logic.impl.FriendLinkServiceImpl;
import com.knife.blog.logic.impl.PhotoServiceImpl;
import com.knife.web.ActionContext;
import com.knife.web.Module;
import com.knife.web.WebForm;
import com.knife.web.tools.AbstractCmdAction;
import com.knife.web.tools.IActiveUser;

abstract class BaseCmdAction extends AbstractCmdAction {
	protected BlogInfo blog;
    private BlogUser bloguser;
	public IActiveUser user;

	protected BlogUserService userService = BlogUserServiceImpl.getInstance();
	
	public Object doBefore(WebForm form, Module module){
		user= CurrentUser.getActiveUser();
		if(user==null){
			return module.findPage("login");
		}else{
			return super.doBefore(form, module);
		}
	}

	public Object doAfter(WebForm form, Module module) {
		SysConfig sysConfig = (SysConfig)ActionContext.getContext().getSession().getServletContext().getAttribute("sysConfig");
		form.addResult("sysConfig", sysConfig);
		form.addResult("dirService", BlogCoreServiceImpl.getInstance());
		form.addResult("cocService", BlogCoreServiceImpl.getInstance());
		form.addResult("currentUser", CurrentUser.getActiveUser());
		form.addResult("photoService", PhotoServiceImpl.getInstance());
		form.addResult("reviewService", BlogReviewServiceImpl.getInstance());
		form.addResult("messageService", BlogMessageServiceImpl.getInstance());
		form.addResult("userService", BlogUserServiceImpl.getInstance());
		form.addResult("linkService",FriendLinkServiceImpl.getInstance());
		form.addResult("resourceService",BlogResourceServiceImpl.getInstance());
		if (blog != null) {
        	bloguser=userService.getBlogUser(blog.getUsername());
        	form.addResult("bloguser", bloguser);
			form.addResult("blog", blog);
			form.addResult("templateName", blog.getTemplate());
		}
		return super.doAfter(form, module);
	}

	public IActiveUser getCurrentUser(WebForm form) {
		// return (IActiveUser) ActionContext.getContext().getSession()
		// .getAttribute("user");
		return com.knife.blog.logic.CurrentUser.getActiveUser();
	}
}